<template>
        <Modal title="人员选择" :transfer=false  v-model="userForSelectModal"  :width=900 @on-cancel="modelClose">
        <Form ref="form2" :model="form2" :label-width="80">
            <Row>
                <Col span="6">
                    <FormItem label="姓名:">
                        <Input type="text" v-model="form2.userName" style="width: 100%"></Input>
                    </FormItem>
                </Col>
                <Col span="6">
                    <FormItem label="登录账号:">
                        <Input type="text" v-model="form2.loginId" style="width: 100%"></Input>
                    </FormItem>
                </Col>
                <Col span="6">
                    <FormItem label="公司：">
                        <Input type="text" v-model="form2.companyName" style="width: 100%"></Input>
                    </FormItem>
                </Col>
                <Col span="6">
                    <FormItem label="部门：">
                       <Input type="text" v-model="form2.orgName" style="width: 100%"></Input>
                    </FormItem>
                </Col>
                <Col span="6">
                    <FormItem label="岗位：">
                         <Input type="text" v-model="form2.positionName" style="width: 100%"></Input>
                    </FormItem>
                </Col>
                <Col span="6">
                    <Button icon="md-search" type="error" style="margin-left: 8px" @click="query">查询</Button>
                    <Button @click="reset" style="margin-left: 8px">重置</Button>
                </Col>
            </Row>
        </Form>

        <Table :loading="loading" class="table" no-data-text="没有查询到相关数据!"  size="small"
               highlight-row stripe border :columns="columns1" width="100%" height="400" :data="userDatas">
        </Table>
        <Page  :page-size="pagedata.pageSize" :total="pagedata.total" :current="pagedata.currentPage" @on-change="pagechange" :page-size-opts="pageSizeArray"
               @on-page-size-change="pageSizeChange" show-total show-sizer show-elevator />

            <div slot="footer">
                <Button class='ivu-btn-save' :disabled="users==[]" :loading="buttonLoading" @click="submit()">确定</Button>
                <Button class='ivu-btn-cancel' @click="modelClose">取消</Button>
            </div>
        </Modal>
</template>

<script>
    import {Api} from '../../../../api/index.js'
    var cascaderDom = () => import('@/components/cascader-dom.vue');
    import { sessionStorage, cookie } from '../../../../common/cacheUtils.js';
    
    export default {
        components: {cascaderDom},
        props: ['orgId','userForSelectModal'],
        data() {
            return {
                loading: false,
                buttonLoading:false,
                userDatas : [],
                form2:{
                    userName: ''
                },
                //分页参数
                pageSizeArray: [10, 20, 50],
                pagedata: {
                    total: 0,
                    pageSize: 10,
                    currentPage: 1,
                },
                columns1: [
                    {
                        width: 60,
                        align: 'center',
                        fixed: 'left',
                        render: (h, params) => {
                            let id = params.row.userId;
                            let flag = false;
                            if (this.currentChoose === id) {
                                flag = true
                            }
                            let self = this
                            return h('div', [
                                h('Radio', {
                                props: {
                                    value: flag
                                },
                                on: {
                                    'on-change': () => {
                                    self.currentChoose = id;
                                    }
                                }
                                })
                            ])
                        }
                    },
                    {
                        title: '姓名',
                        minWidth: 100,
                        align: 'center',
                        key: 'userName'
                    },
                    {
                        title: '登录账号',
                        minWidth: 100,
                        align: 'center',
                        key: 'loginId'
                    },
                    {
                        title: '所属单位',
                        minWidth: 250,
                        align: 'center',
                        key: 'companyName'
                    },   {
                        title: '所属部门',
                        minWidth: 250,
                        align: 'center',
                        key: 'orgName'
                    },
                    {
                        title: '所属岗位',
                        minWidth: 250,
                        align: 'center',
                        render: (h, params) => {
                            return h("span", params.row.position == null?"":params.row.position.positionName);
                        }
                    }
                ],
                users:[],
                currentChoose:""
            }
        },
        mounted: function() {
          
        },
        methods: {
            modelClose(){
                this.$emit('closeModel');
            },
            pagechange(pageNum){
                this.pagedata.currentPage = pageNum;
                this.query();
            },
            pageSizeChange(pageSize) {
                this.pagedata.pageSize = pageSize;
                this.query();
            },
            //查
            query() {
                this.loading = true;
                let data = {
                    fromPage: this.pagedata.currentPage,
                    pageSize: this.pagedata.pageSize,
                    companyName: sessionStorage('userId').getObject('userId').companyName
                }
                data = Object.assign(data,JSON.parse(JSON.stringify(this.form2)));
                this.$http.get(Api.queryUser, {params : data}).then(res => {
                    res.data.data.map(i=>{
                        if(i.orgInfo != null) {
                            i.orgName = i.orgInfo.orgName;
                        }
                    });
                    this.userDatas = res.data.data;
                    this.pagedata.total = res.data.total;
                    this.loading = false;
                }).catch(e => {
                    this.loading = false;
                })
            },
            reset() {
                this.currentChoose = "";
                this.form2 = {
                    userName: ''
                };
                this.pagedata.currentPage = 1;
                this.pagedata.total = 0;
                this.query();
            },
            setOrgId(data) {
                this.form2.orgId = data[data.length - 1];
            },
            submit(){
                 this.userDatas.forEach(item=>{
                     if(item.userId == this.currentChoose){
                        this.buttonLoading=true
                        this.$emit('getSelectData',item);
                        this.users.splice(0);
                        this.$Message.success('成功!');
                        this.buttonLoading=false
                        this.$emit('closeModel');
                     }
                 })
            },
        }
    }
</script>
